<?php
/*
 * Created on 2011-1-15
 * by lixilei
 */

class PublishCtrl extends spController{
	
	/**
	 * 根据编号查询合作商户或商品
	 */
	public function readyForCheckIn(){
		// 获取参数
		$user_id = $_SESSION["userInfo"]["user_id"];
		$number = trim($_POST["number"]);
		
		// 参数为空
		if($number == "" || $number == NULL){
			$this->results = $_SESSION;
			$this->results += array("ret"=>_FAIL, "errMsg"=>_NUMBER_NULL);
			// 记录并调度信息
			$toURL = "checkIn.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "PublishCtrl&readyForCheckIn&error&numberNull", $user_id);
			$this->display($toURL);
			return;
		}
		
		// 执行业务
		$connectionConf = spClass("ConnectionConf", null, CONF_PATH."ConnectionConf.php");
		$webNames = "";
		$weightTmp = 0;
		$websArray = array();
		$weightArray = array();
		foreach($connectionConf->CONNECT_ARRAY as $c){
			// 判断用户是否同步到该站点，并获取影响力
			if($_SESSION["userInfo"][$c["db_colum"]] == 1 && $_POST[$c["db_colum"]] == 1){
				array_push($websArray, $c["db_colum"]);
				$webNames .= $c["name"]._COMMA;
				$bizTmp = spClass($c["biz_name"], null, BIZ_PATH.$c["biz_name"].".php");
				$tmp = (int)($bizTmp->getWeight($_SESSION["userInfo"]["user_id"]));
				$weightTmp += $tmp;
				$weightArray[$c["db_colum"]] = $tmp;
			}
		}
		if($webNames != ""){
			$webNames = substr($webNames, 0, -3);
		}
		
		// 获取商家优惠策略
		$shopBiz = spClass("ShopBiz", null, BIZ_PATH."ShopBiz.php");
		$result = $shopBiz->getShopStatics($number, $weightTmp);
		if($result["ret"] == _SUCCESS){
			// 缓存要同步到的网站、选中的影响力、匹配到的优惠策略
			$_SESSION["publish_to"]["webs"] = $websArray;
			$_SESSION["publish_to"]["weights"] = $weightArray;
			$_SESSION["publish_to"]["statics"] = $result["statics"];
			// 记录并调度信息
			$this->results = $_SESSION;
			$this->results += array(
								"ret"=>_SUCCESS,
								"statics"=>$result["statics"],
								"webNames"=>$webNames,
								"weightTmp"=>$weightTmp
							);
			$toURL = "publish.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "PublishCtrl&readyForCheckIn", $user_id);
			$this->display($toURL);
		} else{
			// 记录并调度信息
			$this->results = $_SESSION;
			$this->results += $result;
			$toURL = "checkIn.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "PublishCtrl&readyForCheckIn&error&getShopStaticError", $user_id);
			$this->display($toURL);
		}
	}
	
	/**
	 * 发布信息
	 */
	 public function publish(){
		// 获取参数
		$user_id = $_SESSION["userInfo"]["user_id"];
		$msg = trim($_POST["message"]);
		
		if($msg == "" || $msg == NULL){
			$this->results = $_SESSION;
			$this->results += array("ret"=>_FAIL, "errMsg"=>_ARGS_ERROR);
			// 记录并调度信息
			$toURL = "checkIn.htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "PublishCtrl&publish&error&argsError", $user_id);
			$this->display($toURL);
			return;
		}
		
		// 执行业务
		$connectionConf = spClass("ConnectionConf", null, CONF_PATH."ConnectionConf.php");
		$result = array();
		foreach($connectionConf->CONNECT_ARRAY as $c){
			// 判断用户是否设置了该站点的同步
			if($_SESSION["userInfo"][$c["db_colum"]] == 1
			 && in_array($c["db_colum"], $_SESSION["publish_to"]["webs"]) == 1){
				$bizTmp = spClass($c["biz_name"], null, BIZ_PATH.$c["biz_name"].".php");
				$tmp = $bizTmp->publish($_SESSION["userInfo"]["user_id"], $msg);
				$result[$c["db_colum"]] = $tmp;
			}
		}
		// 记录同步结果
		$messageBiz = spClass("MessageBiz", null, BIZ_PATH."MessageBiz.php");
		$messageId = $messageBiz->recordMessage($_SESSION["userInfo"]["user_id"],
										 $_SESSION["publish_to"]["statics"]["shop_id"],
										 $_SESSION["publish_to"]["statics"]["shop_statics_id"],
										 $msg, json_encode($_SESSION["publish_to"]["weights"]),
										 json_encode($result));
		$result["message_id"] = json_encode($messageId);
		$result["statics_description"] = $_SESSION["publish_to"]["statics"]["description"];
		// 记录并调度信息
		$this->results = $_SESSION;
		$this->results += array("publish"=>$result);
		$toURL = "publish-result.htm";
		$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
		$dispatchBiz->recordDispatch($toURL, "PublishCtrl&publish&error&argsError", $user_id);
		$this->display($toURL);
	 }	
}
?>